package com.example.tests;

public class ContactData implements Comparable<ContactData> {

	private String id;
	private String firstName;
	private String lastName;
	private String primaryAddress;
	private String homePhoneNr;
	private String mobilePhoneNr;
	private String workPhoneNr;
	private String firstEmailAddress;
	private String secondEmailAddress;
	private String birthdayDay;
	private String birthdayMonth;
	private String birthdayYear;
	private String contactGroup;
	private String secondaryAddress;
	private String secondaryHomePhoneNr;

	public ContactData(){
		
	}
	
	public ContactData(String firstName, String lastName,
			String primaryAddress, String homePhoneNr, String mobilePhoneNr,
			String workPhoneNr, String firstEmailAddress,
			String secondEmailAddress, String birthdayDay,
			String birthdayMonth, String birthdayYear, String contactGroup,
			String secondaryAddress, String secondaryHomePhoneNr) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.primaryAddress = primaryAddress;
		this.homePhoneNr = homePhoneNr;
		this.mobilePhoneNr = mobilePhoneNr;
		this.workPhoneNr = workPhoneNr;
		this.firstEmailAddress = firstEmailAddress;
		this.secondEmailAddress = secondEmailAddress;
		this.birthdayDay = birthdayDay;
		this.birthdayMonth = birthdayMonth;
		this.birthdayYear = birthdayYear;
		this.contactGroup = contactGroup;
		this.secondaryAddress = secondaryAddress;
		this.secondaryHomePhoneNr = secondaryHomePhoneNr;
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((firstEmailAddress == null) ? 0 : firstEmailAddress.hashCode());
		result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
		result = prime * result
				+ ((homePhoneNr == null) ? 0 : homePhoneNr.hashCode());
		result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ContactData other = (ContactData) obj;
		if (lastName == null) {
			if (other.lastName != null)
				return false;
		} else if (!lastName.equals(other.lastName))
			return false;
		if (firstName == null) {
			if (other.firstName != null)
				return false;
		} else if (!firstName.equals(other.firstName))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "ContactData [firstName=" + firstName + ", lastName=" + lastName
				+ ", homePhoneNr=" + homePhoneNr + ", firstEmailAddress="
				+ firstEmailAddress + "]";
	}

	@Override
	public int compareTo(ContactData other) {
		final int EQUAL = 0;
		
		if (this == other) return EQUAL;
		
		int comparison = this.lastName.toLowerCase().compareTo(other.lastName.toLowerCase());
		if (comparison != EQUAL) return comparison;
		
		comparison = this.firstName.toLowerCase().compareTo(other.firstName.toLowerCase());
		if (comparison != EQUAL) return comparison;
		
		assert this.equals(other) : "compareTo inconsistent with equals";
		
		return EQUAL;
	}

	// update with values of non-empty fields of contact, other fields are not touched
	public void updateContact(ContactData contact, boolean formType) {
		if (contact.firstName.isEmpty() == false) {this.firstName = contact.firstName;}
		if (contact.lastName.isEmpty() == false) {this.lastName = contact.lastName;}
		if (contact.primaryAddress.isEmpty() == false) {this.primaryAddress = contact.primaryAddress;}
		if (contact.homePhoneNr.isEmpty() == false) {this.homePhoneNr = contact.homePhoneNr;}
		if (contact.mobilePhoneNr.isEmpty() == false) {this.mobilePhoneNr = contact.mobilePhoneNr;}
		if (contact.workPhoneNr.isEmpty() == false) {this.workPhoneNr = contact.workPhoneNr;}
		if (contact.firstEmailAddress.isEmpty() == false) {this.firstEmailAddress = contact.firstEmailAddress;}
		if (contact.secondEmailAddress.isEmpty() == false) {this.secondEmailAddress = contact.secondEmailAddress;}
		if (contact.birthdayDay.isEmpty() == false) {this.birthdayDay = contact.birthdayDay;}
		if (contact.birthdayMonth.isEmpty() == false) {this.birthdayMonth = contact.birthdayMonth;}
		if (contact.birthdayYear.isEmpty() == false) {this.birthdayYear = contact.birthdayYear;}
		if (formType) {
			if (contact.contactGroup.compareTo("[none]") == 0) {this.contactGroup = contact.contactGroup;}
		}
		if (contact.secondaryAddress.isEmpty() == false) {this.secondaryAddress = contact.secondaryAddress;}
		if (contact.secondaryHomePhoneNr.isEmpty() == false) {this.secondaryHomePhoneNr = contact.secondaryHomePhoneNr;}
	}

	public ContactData withId(String id) {
		this.id = id;
		return this;
	}

	public ContactData withFirstName(String firstName) {
		this.firstName = firstName;
		return this;
	}

	public ContactData withLastName(String lastName) {
		this.lastName = lastName;
		return this;
	}

	public ContactData withPrimaryAddress(String primaryAddress) {
		this.primaryAddress = primaryAddress;
		return this;
	}

	public ContactData withHomePhoneNr(String homePhoneNr) {
		this.homePhoneNr = homePhoneNr;
		return this;
	}

	public ContactData withMobilePhoneNr(String mobilePhoneNr) {
		this.mobilePhoneNr = mobilePhoneNr;
		return this;
	}

	public ContactData withWorkPhoneNr(String workPhoneNr) {
		this.workPhoneNr = workPhoneNr;
		return this;
	}

	public ContactData withFirstEmailAddress(String firstEmailAddress) {
		this.firstEmailAddress = firstEmailAddress;
		return this;
	}

	public ContactData withSecondEmailAddress(String secondEmailAddress) {
		this.secondEmailAddress = secondEmailAddress;
		return this;
	}

	public ContactData withBirthdayDay(String birthdayDay) {
		this.birthdayDay = birthdayDay;
		return this;
	}

	public ContactData withBirthdayMonth(String birthdayMonth) {
		this.birthdayMonth = birthdayMonth;
		return this;
	}

	public ContactData withBirthdayYear(String birthdayYear) {
		this.birthdayYear = birthdayYear;
		return this;
	}

	public ContactData withContactGroup(String contactGroup) {
		this.contactGroup = contactGroup;
		return this;
	}

	public ContactData withSecondaryAddress(String secondaryAddress) {
		this.secondaryAddress = secondaryAddress;
		return this;
	}

	public ContactData withSecondaryHomePhoneNr(String secondaryHomePhoneNr) {
		this.secondaryHomePhoneNr = secondaryHomePhoneNr;
		return this;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getPrimaryAddress() {
		return primaryAddress;
	}

	public void setPrimaryAddress(String primaryAddress) {
		this.primaryAddress = primaryAddress;
	}

	public String getHomePhoneNr() {
		return homePhoneNr;
	}

	public void setHomePhoneNr(String homePhoneNr) {
		this.homePhoneNr = homePhoneNr;
	}

	public String getMobilePhoneNr() {
		return mobilePhoneNr;
	}

	public void setMobilePhoneNr(String mobilePhoneNr) {
		this.mobilePhoneNr = mobilePhoneNr;
	}

	public String getWorkPhoneNr() {
		return workPhoneNr;
	}

	public void setWorkPhoneNr(String workPhoneNr) {
		this.workPhoneNr = workPhoneNr;
	}

	public String getFirstEmailAddress() {
		return firstEmailAddress;
	}

	public void setFirstEmailAddress(String firstEmailAddress) {
		this.firstEmailAddress = firstEmailAddress;
	}

	public String getSecondEmailAddress() {
		return secondEmailAddress;
	}

	public void setSecondEmailAddress(String secondEmailAddress) {
		this.secondEmailAddress = secondEmailAddress;
	}

	public String getBirthdayDay() {
		return birthdayDay;
	}

	public void setBirthdayDay(String birthdayDay) {
		this.birthdayDay = birthdayDay;
	}

	public String getBirthdayMonth() {
		return birthdayMonth;
	}

	public void setBirthdayMonth(String birthdayMonth) {
		this.birthdayMonth = birthdayMonth;
	}

	public String getBirthdayYear() {
		return birthdayYear;
	}

	public void setBirthdayYear(String birthdayYear) {
		this.birthdayYear = birthdayYear;
	}

	public String getContactGroup() {
		return contactGroup;
	}

	public void setContactGroup(String contactGroup) {
		this.contactGroup = contactGroup;
	}

	public String getSecondaryAddress() {
		return secondaryAddress;
	}

	public void setSecondaryAddress(String secondaryAddress) {
		this.secondaryAddress = secondaryAddress;
	}

	public String getSecondaryHomePhoneNr() {
		return secondaryHomePhoneNr;
	}

	public void setSecondaryHomePhoneNr(String secondaryHomePhoneNr) {
		this.secondaryHomePhoneNr = secondaryHomePhoneNr;
	}

}